package com.suirich.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suirich.bean.ProductCategories;
import com.suirich.service.BaseService;
import com.suirich.service.ProductCategoriesService;

@Service
@Transactional
public class ProductCategoriesServiceImpl extends BaseService implements ProductCategoriesService {

	public void create(ProductCategories productCategories) {
		this.productCategoriesDao.create(productCategories);
	}

    public void saveOrUpdate(ProductCategories productCategories) {
        this.productCategoriesDao.saveOrUpdate(productCategories);
    }
    
	public ProductCategories read(Long id) {
		return this.productCategoriesDao.read(id);
	}

	public void delete(ProductCategories productCategories) {
	    if(productCategories.getParentId() == 0){
	        List<ProductCategories> categoriesList = this.productCategoriesDao.getProductCategoriesList(productCategories.getId());
	        
	        for(ProductCategories category : categoriesList){
	            this.productDao.deleteProductsByCategoryId(category.getId());
	        }
	        
	        this.productCategoriesDao.deleteProductCategoriesByParentId(productCategories.getId());
	    }else{
	        this.productDao.deleteProductsByCategoryId(productCategories.getId());
	    }
        this.productCategoriesDao.delete(productCategories);
        
	}

	public List<ProductCategories> getProductCategoriesList(Long parentId) {
		return this.productCategoriesDao.getProductCategoriesList(parentId);
	}

    public void update(ProductCategories productCategories) {
        this.productCategoriesDao.update(productCategories);
    }


}
